<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Relations\TeachesTrait;
use Illuminate\Support\Facades\DB;
use App\Models\Relations\ValidateTrait;
use App\Models\Relations\CommonTrait;
/**
 *
 * @author Joker
 *
 */
class Company extends Model
{
    use TeachesTrait,ValidateTrait,CommonTrait;
    protected $table='company';

    protected $primaryKey = 'id';

    protected $fillable = array('id','name','signature','intro','email','phone','scale_id',
        'main_page','develop_id','investment','auth_id','is_teaches','logo','tel','nature','is_validate','validated_at','created_uid'
    );
    
    protected $dates = ['validated_at','teaches_at'];

    /**
     * 获取认证信息
     */
    public  function authes()
    {
        return $this->hasOne('App\Models\CompanyAuth','auth_id','auth_id');
    }

    /**
     *  获取规模信息
     */
    public function scales()
    {
        return $this->hasOne('App\Models\CompanyScale','id','scale_id');
    }

    /**
     * 获取发展阶段
     */
    public function develops()
    {
        return $this->hasOne('App\Models\CompanyDevelop','id','develop_id');
    }

    /**
     * 获取公司的所属行业
     */
    public function types()
    {
        return $this->hasMany('App\Models\CompanyTypeLink','company_id','id');
    }

    /**
     * 获取企业号下的账户信息
     */
    public function users() {
        return $this->hasMany('App\User','company_id','id');
    }

    /**
     * 获取企业号下的标签
     */
    public function tags()
    {
        return $this->hasMany('App\Models\CompanyTags','company_id','id');
    }

    /**
     * 获取试卷编号，一对多
     */
    public function examinations()
    {
        return $this->hasMany('App\Models\Examination','company_id','id');
    }

    /**
     * 获取企业认证信息
     * @return [type] [description]
     */
    public function authinfo()
    {
        return $this->hasOne('App\Models\CompanyAuthInfo', 'company_id', 'id');
    }
    /**
     * 获取企业工作信息
     */
    public function jobsinfo()
    {
        return $this->hasMany('App\Models\CompanyJobs','company_id','id');
    }

    /**
     * 获取公司地址信息
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function addressinfo()
    {
        return $this->hasOne('App\Models\CompanyAddress','company_id','id');
    }
    
    /**
     * 内训 
     */
    public function teaches()
    {
        return $this->hasMany('App\Models\TeachesCompany','company_id','id');
    }

    public static  function getCompanyName($id)
    {
         $company = DB::table('company')->where('id','=',$id)->first();
         if(!empty($company))
         {
             return $company->name;
         }
         else
         {
             return '';
         }
    }
    
    /**
     * 获取企业列表数据
     */
    public function getList($skip,$pagesize,$where,$sort = 0)
    {
        $query = DB::table($this->table)
                    ->select('company.*');
        if($where['areaid']>0)
        {
            $query->leftJoin('company_address','company.id','=','company_address.company_id')
                  ->where('company_address.city','=',$where['areaid']);
        }
        if($where['comtype']>0)
        {
            $query->leftJoin('company_type_link','company.id','=','company_type_link.company_id')
                  ->where('company_type_link.type_id','=',$where['comtype']);
        }
        if($where['develop']>0)
        {
            $query->where('company.develop_id','=',$where['develop']);
        }
        if($where['keyword'])
        {
            $query-> where('name','like','%'.$where['keyword'].'%');
        }
        switch ($sort){
            case 1:
                $query->orderBy('company.updated_at','desc');break;
            default:
                $query->orderBy('company.auth_at','desc')->orderBy('company.created_at','desc');

        }
        $result = $query->orderBy('company.id','desc')->skip($skip)->take($pagesize)->get();

        return $result;

    }

}
